package com.qdsgjsfk.vision.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;

import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.NetUtil;
import com.rest.business.RestProxy;
import com.rest.response.Student;
import com.rest.response.StudentResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wj.android.colorcardview.CardView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by HP on 2019/9/2.
 */

public class CollectSearchStudentActivity extends BaseActivity {

    @BindView(R.id.recyclerView_student_list)
    RecyclerView recyclerView_student_list;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R.id.ed_search)
    EditText ed_search;

    MyAdapter myAdapter;
    Bundle bundle;
    String taskId;
    List<Student> list = new ArrayList<>();

    @Override
    protected void initViews() {
        myAdapter = new MyAdapter();
        recyclerView_student_list.setLayoutManager(new LinearLayoutManager(this));
        recyclerView_student_list.setAdapter(myAdapter);
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getStudentList();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

            }
        });
        ed_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                getStudentList();
                return true;
            }
        });
    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra("taskId");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_search;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.StudentHolder> {

        @Override
        public MyAdapter.StudentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyAdapter.StudentHolder(getLayoutInflater().inflate(R.layout.item_collect_student, parent, false));
        }

        @Override
        public void onBindViewHolder(MyAdapter.StudentHolder holder, final int position) {
            ((StudentHolder) holder).tv_name.setText(list.get(position).studentName);
            ((StudentHolder) holder).container.setBackgroundResource(R.drawable.round_white);
            ((StudentHolder) holder).tv_name.setText(list.get(position).studentName);
            if (list.get(position).isFinish == 0) {
                ((StudentHolder) holder).tv_status.setText("未采集");
                ((StudentHolder) holder).tv_status.setTextColor(Color.parseColor("#F54C4C"));
            } else {
                ((StudentHolder) holder).tv_status.setText("已采集");
                ((StudentHolder) holder).tv_status.setTextColor(Color.parseColor("#3EBFC7"));
            }
            ((StudentHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).isFinish == 1) {
                        Bundle bundle = new Bundle();
                        bundle.putString("studentId", list.get(position).studentId);
                        bundle.putString("studentName", list.get(position).studentName);
                        bundle.putString("faceImg",list.get(position).faceImg);
                        bundle.putInt("type", 1);
                        bundle.putString("taskId",taskId);
                        startActivity( FaceCheckActivity.class, bundle);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("studentId", list.get(position).studentId);
                        bundle.putString("studentName", list.get(position).studentName);
                        startActivity( FaceDetectorInfoActivity.class, bundle);
                    }
                }
            });

        }


        @Override
        public int getItemCount() {
            return list.size();
        }


        class StudentHolder extends RecyclerView.ViewHolder {
            TextView tv_name, tv_status;
            CardView container;
            View line;

            public StudentHolder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                container = itemView.findViewById(R.id.container);
                line = itemView.findViewById(R.id.line);
            }
        }


    }

    private void getStudentList() {
        RestProxy.getInstance().getStudentList(taskId, "", 0+"", ed_search.getText().toString().trim(), new Observer<StudentResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(StudentResponse studentResponse) {
                list.clear();
                list.addAll(studentResponse.data.classStudentList.list);
                myAdapter.notifyDataSetChanged();
                if(list.size()==0){
                    Toast.makeText( CollectSearchStudentActivity.this,"暂无学生",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                refreshLayout.finishRefresh();
                NetUtil.onError(e, CollectSearchStudentActivity.this);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh();
            }
        });
    }



}
